local extension = Package("tgong_xx")
extension.extensionName = "tshu"

Fk:loadTranslationTable{
  ["tgong_xx"] = "天宫.星宿",
  ["tshuxx"] = "星宿",
}

local U = require "packages/utility/utility"

local xingxiang = General(extension, "tshuxx__xingxiang","jin",10)
xingxiang.hidden = true
local zhanbu = fk.CreateTriggerSkill{
  name = "tshu__zhanbu",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.BeforeTurnStart},
  can_trigger = function(self, event, target, player, data)
    local room = player.room
    if target == player and player:hasSkill(self) and player:usedSkillTimes(self.name, Player.HistoryRound) == 0 then
      local tar = {}
      for _, p in ipairs(room.alive_players) do
        if p.role == "rebel" then
          table.insertIfNeed(tar,p)
        end
      end
      self.cost_data = tar
      return #tar > 0
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = table.random(self.cost_data)
    player:skip(Player.Judge)
    player:skip(Player.Draw)
    player:skip(Player.Play)
    player:skip(Player.Discard)
    room:doIndicate(player.id,{to.id})
    to:gainAnExtraTurn()
  end,
}
local zhanbu_trigger = fk.CreateTriggerSkill{
  name = "#tshu__zhanbu_trigger",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageInflicted then
        return target == player and player:hasSkill(self) 
    end
  end,
  on_use = function(self, event, target, player, data)
      return true
  end,
}
local zhanbu_buff = fk.CreateProhibitSkill{
  name = "#tshu__zhanbu_buff",
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to, card)
      return to:hasSkill(self) and from.role ~= to.role
  end,
}
local zhanbu_prohibit = fk.CreateProhibitSkill{
  name = "#zhanbu_prohibit",
  prohibit_use = function(self, player, card)
      return player:hasSkill(self) 
  end,
  prohibit_response = function(self, player, card)
      return player:hasSkill(self) 
  end,
}
zhanbu:addRelatedSkill(zhanbu_trigger)
zhanbu:addRelatedSkill(zhanbu_buff)
zhanbu:addRelatedSkill(zhanbu_prohibit)
xingxiang:addSkill(zhanbu)
Fk:loadTranslationTable{
  ["tshuxx__xingxiang"] = "星象",
  ["tshu__zhanbu"] = "占卜",
  [":tshu__zhanbu"] = "锁定技，每轮限一次，你的回合开始前，你跳过所有阶段并令随机一名玩家获得一个额外的回合;<br>防止你受到的所有伤害，你无法使用或打出牌，你不会成为其他角色使用牌的目标。",
  ["#tshu__zhanbu_trigger"] = "占卜",
}

local jiaomujiao = General(extension, "tshuxx__jiaomujiao","wu",8)
jiaomujiao.hidden = true
local jiaomu = fk.CreateTriggerSkill{
  name = "tshu__jiaomu",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.card.type == Card.TypeTrick
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = {player}
    for _, p in ipairs(room.alive_players) do
      if p.faceup and ((player.role == "rebel" and p.role == "rebel") or ((player.role == "lord" or player.role == "loyalist") and (p.role == "lord" or p.role == "loyalist"))) then
        table.insertIfNeed(targets,p)
      end
    end
    if #targets > 0 then
      table.random(targets):drawCards(1,self.name)
    end
  end,
}
jiaomujiao:addSkill(jiaomu)
Fk:loadTranslationTable{
  ["tshuxx__jiaomujiao"] = "角木蛟",
  ["tshu__jiaomu"] = "角木",
  [":tshu__jiaomu"] = "锁定技，当你使用锦囊牌后，你令随机一名武将牌正面向上的友方角色摸一张牌。",

  ["~tshuxx__jiaomujiao"] = "业阳涸泽，青蛟失溪。",
}

local kangjinlong = General(extension, "tshuxx__kangjinlong","wu",8)
kangjinlong.hidden = true
local kangjin = fk.CreateTriggerSkill{
  name = "tshu__kangjin",
  anim_type = "drawcard",
  frequency = Skill.Compulsory,
  events = {fk.TargetConfirmed},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.card.type == Card.TypeTrick 
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = {player}
    for _, p in ipairs(room.alive_players) do
      if p.faceup and ((player.role == "rebel" and p.role == "rebel") or ((player.role == "lord" or player.role == "loyalist") and (p.role == "lord" or p.role == "loyalist"))) then
        table.insertIfNeed(targets,p)
      end
    end
    if #targets > 0 then
      table.random(targets):drawCards(1,self.name)
    end
  end,
}
kangjinlong:addSkill(kangjin)
Fk:loadTranslationTable{
  ["tshuxx__kangjinlong"] = "亢金龙",
  ["tshu__kangjin"] = "亢金",
  [":tshu__kangjin"] = "锁定技，当你成为锦囊牌的目标后，你令随机一名武将牌正面向上的友方角色摸一张牌。",

  ["$tshu__kangjin"] = "金龙亢升，天动相祸。",
  ["~tshuxx__kangjinlong"] = "干戈寥落，亢龙有悔。",
}

local yihuoshe = General(extension, "tshuxx__yihuoshe","shu",8,8,General.Female)
yihuoshe.hidden = true
local yihuo = fk.CreateTriggerSkill{
  name = "tshu__yihuo",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      return target == player and player:hasSkill(self) and data.damageType ~= fk.FireDamage
    elseif event == fk.Damage then
      return target == player and player:hasSkill(self) and data.damageType == fk.FireDamage and data.to and not data.to:isNude()
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      player:broadcastSkillInvoke(self.name)
      room:notifySkillInvoked(player,self.name,"offensive")
       data.damageType = fk.FireDamage
    elseif event == fk.Damage then
      player:broadcastSkillInvoke(self.name)
      room:notifySkillInvoked(player,self.name,"control")
      room:askForDiscard(data.to,1,1,true,self.name,false)
    end
  end,
}
yihuoshe:addSkill(yihuo)
Fk:loadTranslationTable{
  ["tshuxx__yihuoshe"] = "翼火蛇",
  ["tshu__yihuo"] = "翼火",
  [":tshu__yihuo"] = "锁定技，你造成的伤害视为火焰伤害；当你造成火焰伤害后，令受伤角色弃置一张牌。",
  ["$tshu__yihuo"] = "鹏蛟展翼，形跃如火。",
  ["~tshuxx__yihuoshe"] = "神火即熄，阳翼何展。",
}

local xingrima = General(extension, "tshuxx__xingrima","shu",8,8)
xingrima.hidden = true
local xingri = fk.CreateTriggerSkill{
  name = "tshu__xingri",
  mute = true,
  frequency = Skill.Compulsory,
  events = {fk.DamageCaused,fk.Damage},
  can_trigger = function(self, event, target, player, data)
    if event == fk.DamageCaused then
      return target == player and player:hasSkill(self) and data.damageType ~= fk.FireDamage
    elseif event == fk.Damage then
      return target == player and player:hasSkill(self) and data.damageType == fk.FireDamage and not player.dead
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.DamageCaused then
      player:broadcastSkillInvoke(self.name)
      room:notifySkillInvoked(player,self.name,"offensive")
       data.damageType = fk.FireDamage
    elseif event == fk.Damage then
      player:broadcastSkillInvoke(self.name)
      room:notifySkillInvoked(player,self.name,"drawcard")
      player:drawCards(1,self.name)
    end
  end,
}
xingrima:addSkill(xingri)
Fk:loadTranslationTable{
  ["tshuxx__xingrima"] = "星日马",
  ["tshu__xingri"] = "星日",
  [":tshu__xingri"] = "锁定技，你造成的伤害视为火焰伤害；当你造成火焰伤害后，你摸一张牌。",
  ["$tshu__xingri"] = "踏星逐日，流焰漫天。",
  ["~tshuxx__xingrima"] = "马失前蹄，星陨大荒",
}

local kuimulang = General(extension, "tshuxx__kuimulang","qun",8,8)
kuimulang.hidden = true
local kuimu = fk.CreateTriggerSkill{
  name = "tshu__kuimu",
  anim_type = "offensive",
  frequency = Skill.Compulsory,
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if player.dead then break end
      if ((player.role == "rebel" and p.role ~= "rebel") or ((player.role == "lord" or player.role == "loyalist") and (p.role ~= "lord" and p.role ~= "loyalist"))) then
        room:damage({
          from = player,
          to = p,
          damage = 1,
          damageType = fk.NormalDamage,
          skillName = self.name,
        })
      end
    end
  end,
}
kuimulang:addSkill(kuimu)
Fk:loadTranslationTable{
  ["tshuxx__kuimulang"] = "奎木狼",
  ["tshu__kuimu"] = "奎木",
  [":tshu__kuimu"] = "锁定技，每当你使用【杀】造成伤害后，你对敌方所有角色各造成一点伤害。",
  ["$tshu__kuimu"] = "奔狼如野，其势如林。",
  ["~tshuxx__kuimulang"] = "孤狼不长，独木难生。",
}

local canshuiyuan = General(extension, "tshuxx__canshuiyuan","qun",8,8)
canshuiyuan.hidden = true
local canshui = fk.CreateTriggerSkill{
  name = "tshu__canshui",
  anim_type = "support",
  frequency = Skill.Compulsory,
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for _, p in ipairs(room.alive_players) do
      if player.dead then break end
      if p:isWounded() and ((player.role == "rebel" and p.role == "rebel") or ((player.role == "lord" or player.role == "loyalist") and (p.role == "lord" or p.role == "loyalist"))) then
        room:recover({
          who = p,
          num = 1,
          recoverBy = player,
          skillName = self.name
        })
      end
    end
  end,
}
canshuiyuan:addSkill(canshui)
Fk:loadTranslationTable{
  ["tshuxx__canshuiyuan"] = "参水猿",
  ["tshu__canshui"] = "参水",
  [":tshu__canshui"] = "锁定技，每当你使用【杀】造成伤害后，你令所有友方角色各回复一点体力。",
  ["$tshu__canshui"] = "大川东涌，摧城拔山。",
  ["~tshuxx__canshuiyuan"] = "..竭泽而毙。",
}

local doumuxie = General(extension, "tshuxx__doumuxie","wei",8,8)
doumuxie.hidden = true
local doumu = fk.CreateTriggerSkill{
  name = "tshu__doumu",
  anim_type = "offensive",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player.phase ~= Player.Draw and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
          if (move.to == player.id and move.toArea == Card.PlayerHand) then
            return true
          elseif  move.from == player.id  then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, p in ipairs(room.alive_players) do
      if ((player.role == "rebel" and p.role ~= "rebel") or ((player.role == "lord" or player.role == "loyalist") and (p.role ~= "lord" and p.role ~= "loyalist"))) then
        table.insertIfNeed(targets,p.id)
      end
    end
    if #targets > 0 then
      local tar = room:askForChoosePlayers(player,targets,1,1,"斗木：你可以选择一名敌方角色，随机弃置其一张牌并视为对其使用一张【决斗】。",self.name,true)
      if #tar > 0 then
        self.cost_data = tar[1]
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(self.cost_data)
    if not tar:isNude() then
      local cards = tar:getCardIds("he")
      room:moveCardTo(table.random(cards,1),Card.DiscardPile,player,fk.ReasonDiscard)
    end
    if not tar.dead and not player.dead then
      room:useVirtualCard("duel", nil, player, {tar}, self.name)
    end
  end,
}
doumuxie:addSkill(doumu)
Fk:loadTranslationTable{
  ["tshuxx__doumuxie"] = "斗木獬",
  ["tshu__doumu"] = "斗木",
  [":tshu__doumu"] = "锁定技，每回合限一次，当你于摸牌阶段外获得或失去手牌时，你可以选择一名敌方角色，随机弃置其一张牌并视为对其使用一张【决斗】。",
  ["$tshu__doumu"] = "侵袭力缴，无坚不摧。",
  ["~tshuxx__doumuxie"] = "..进退为骨。",
}

local weiyueyan = General(extension, "tshuxx__weiyueyan","wei",8,8,General.Female)
weiyueyan.hidden = true
local weiyue = fk.CreateTriggerSkill{
  name = "tshu__weiyue",
  anim_type = "offensive",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player.phase ~= Player.Draw and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 then
      for _, move in ipairs(data) do
          if (move.to == player.id and move.toArea == Card.PlayerHand) then
            return true
          elseif  move.from == player.id  then
            for _, info in ipairs(move.moveInfo) do
              if info.fromArea == Card.PlayerHand then
                return true
              end
            end
          end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = {}
    for _, p in ipairs(room.alive_players) do
      if ((player.role == "rebel" and p.role ~= "rebel") or ((player.role == "lord" or player.role == "loyalist") and (p.role ~= "lord" and p.role ~= "loyalist"))) then
        table.insertIfNeed(targets,p.id)
      end
    end
    if #targets > 0 then
      local tar = room:askForChoosePlayers(player,targets,1,1,"危月：你可以对一名敌方角色造成一点伤害。",self.name,true)
      if #tar > 0 then
        self.cost_data = tar[1]
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tar = room:getPlayerById(self.cost_data)
    room:damage({
      from = player,
      to = tar,
      damage = 1,
      damageType = fk.NormalDamage,
      skillName = self.name,
    })
  end,
}
weiyueyan:addSkill(weiyue)
Fk:loadTranslationTable{
  ["tshuxx__weiyueyan"] = "危月燕",
  ["tshu__weiyue"] = "危月",
  [":tshu__weiyue"] = "锁定技，每回合限一次，当你于摸牌阶段外获得或失去手牌时，你可以对一名敌方角色造成一点伤害。",
  ["$tshu__weiyue"] = "残月肃潇，夜翼铩羽。",
  ["~tshuxx__weiyueyan"] = "月落星陨，危月失巢。",
}

return extension